<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 服务管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.serviceId" placeholder="服务ID" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
                <el-input v-model="query.port" placeholder="端口号" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="el-button" icon="el-icon-delete" @click="handleReset">重置</el-button>
                <div class="right-items" style="float: right;">
                    <el-button type="primary" icon="el-icon-plus" @click="handleAdd">上线</el-button>
                </div>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="serviceId" label="服务ID" width="200" align="center"></el-table-column>
                <el-table-column prop="serviceName" label="服务名" width="200" align="center"></el-table-column>
                <el-table-column prop="host" label="IP地址" align="center"></el-table-column>
                <el-table-column prop="port" label="端口号" align="center"></el-table-column>
                <el-table-column prop="homePageUrl" label="访问地址" align="center"></el-table-column>
                <el-table-column v-if="show" prop="state" label="运行状态" align="center"></el-table-column>
                <el-table-column prop="tag" label="运行状态" width="100" filter-placement="bottom-end" align="center">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.state != 'UP' ? 'primary' : 'success'" disable-transitions>{{scope.row.state}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="250" align="center">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.state=='UP'"
                                   type="text"
                                   icon="el-icon-video-pause"
                                   @click="handleDown(scope.$index, scope.row)"
                        >下线</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="30%" :close-on-click-modal="false">
            <el-form ref="addForm" :rules="addFormRules" :model="addForm" label-width="100px">
                <el-form-item label="IP地址：" prop="addr">
                    <el-input v-model="addForm.addr"></el-input>
                </el-form-item>
                <el-form-item label="端口号：" prop="port">
                    <el-input v-model="addForm.port"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleUp">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import { eurekaInfoData, handleStart, handleStop } from '../../../../api/system/eureka/eurekaInfo';
    export default {
        name: 'eurekaInfo',
        data() {
            return {
                show: false,
                query: {
                    serviceId: '',
                    port: '',
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                pageTotal: 0,
                idx: -1,
                id: -1,
                addVisible: false,
                addForm: {},
                addFormRules: {
                    addr: [
                        { required: true, message: '请输入IP地址', trigger: 'blur' }
                    ],
                    port: [
                        { required: true, message: '请输入端口号', trigger: 'blur' }
                    ]
                },
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 获取后台数据
            getData() {
                eurekaInfoData(this.query).then(res => {
                    if (res.success == true){
                        this.tableData = res.data;
                        this.pageTotal = res.total;
                    } else {
                        this.$message.error("查询失败！");
                        this.tableData = [];
                        this.pageTotal = 0;
                    }
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 触发重置按钮
            handleReset() {
                this.query.serviceId = "";
                this.query.port = "";
                this.getData();
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
            /*下线*/
            handleDown(index, row){
                handleStop({
                    addr:row.host,
                    port:row.port
                }).then(res =>{
                    if (res.success == true){
                        this.$message.success("服务停止成功！");
                        this.getData();
                    } else {
                        this.$message.error("服务停止失败！");
                        this.getData();
                    }
                });
            },
            handleAdd() {
                this.addForm = {};
                this.addVisible = true;
            },
            /*上线*/
            handleUp(){
                handleStart({
                    addr:this.addForm.addr,
                    port:this.addForm.port
                }).then(res =>{
                    if (res.success == true){
                        this.$message.success("服务启动成功！");
                        this.getData();
                    } else {
                        this.$message.error("服务启动失败！");
                        this.getData();
                    }
                });
                this.addVisible = false;
            },
            /*表单重置*/
            resetForm(refname){
                if (this.$refs[refname] !== undefined){
                    this.$refs[refname].resetFields();
                }
            },
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
        /*border-radius 属性允许您为元素添加圆角边框！*/
        border-radius:6px;
        background-clip:padding-box;
        border-top-left-radius: 6px;
        border-top-right-radius: 6px;
        border-bottom-right-radius: 6px;
        border-bottom-left-radius: 6px;
    }
    /*头像上传*/
    .user-header{
        position: relative;
        display: inline-block;
    }
    .user-header-com{
        width: 100px;
        height: 100px;
        border-radius: 50%;
        display: inline-block;
    }
    .header-upload-btn{
        position: absolute;
        left: 0;
        top: 0;
        opacity: 0;
        /* 通过定位把input放在img标签上面，通过不透明度隐藏 */
    }
</style>
